using System.Text;
using NUnit.Framework;

namespace ProjectEuler.Core
{
    public class Problem40 : IProjectEulerProblem
    {
        protected string GetAtLeastThisManyDigitsByAppendingIntegers(int n)
        {
            var digits = new StringBuilder("");
            int i = 1;
            while(digits.Length < n)
            {
                digits.Append(i);
                i++;
            }
            return digits.ToString();
        }

        protected int GivenExpression()
        {
            var d = GetAtLeastThisManyDigitsByAppendingIntegers(1000000);
            var result = int.Parse(d[0].ToString());
            result *= int.Parse(d[9].ToString());
            result *= int.Parse(d[99].ToString());
            result *= int.Parse(d[999].ToString());
            result *= int.Parse(d[9999].ToString());
            result *= int.Parse(d[99999].ToString());
            result *= int.Parse(d[999999].ToString());
            return result;
        }

        public int Number
        {
            get { return 40; }
        }

        public string Description
        {
            get { return "Find the value of the given expression"; }
        }

        public string Answer
        {
            get { return GivenExpression().ToString(); }
        }
    }

    [TestFixture]
    public class Problem40Tests: Problem40
    {
        [Test]
        public void The_12th_digit_is_1()
        {
            var str = GetAtLeastThisManyDigitsByAppendingIntegers(50);
            Assert.AreEqual(str[11],'1');
        }
    }
}